package com.sky.service.Impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.OverviewDishesVO;
import com.sky.vo.OverviewOrdersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;



    public BusinessDataVO getBusiness(LocalDateTime begin, LocalDateTime end) {
        BusinessDataVO businessDataVO = new BusinessDataVO();
        Integer newUsers = reportMapper.countNewUserByDate(begin, end);
        businessDataVO.setNewUsers(newUsers);

        //今日營業額
        HashMap map = new HashMap();
        map.put("status", Orders.COMPLETED);
        map.put("begin", begin);
        map.put("end", end);
        Double turnover = reportMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;
        businessDataVO.setTurnover(turnover);

        //有效訂單數
        Integer validOrderCount = reportMapper.countValidOrders(begin, end);
        businessDataVO.setValidOrderCount(validOrderCount);

        //訂單完成率
        Integer orderCounts = reportMapper.countOrder(begin, end);
        double orderCompletionRate = orderCounts == 0 ? 0.0 : (double) validOrderCount / orderCounts;
        businessDataVO.setOrderCompletionRate(orderCompletionRate);

        //平均客單價
        Double unitPrice = validOrderCount == 0 ? 0.0 : turnover / validOrderCount;
        businessDataVO.setUnitPrice(unitPrice);

        return businessDataVO;

    }

    @Override
    public BusinessDataVO getBusinessData() {
        LocalDate now = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(now, LocalTime.MAX);
        return getBusiness(begin, end);
    }

    @Override
    public OverviewOrdersVO getOverviewOrders() {
        OverviewOrdersVO overviewOrdersVO = new OverviewOrdersVO();

        LocalDate now = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(now, LocalTime.MAX);

        //全部訂單
        Integer allOrderCount = orderMapper.countAllOrdersWithDate(begin, end);
        overviewOrdersVO.setAllOrders(allOrderCount);

        //已取消訂單
        Integer cancelledOrders = orderMapper.countByStatusWithDate(Orders.CANCELLED, begin, end);
        overviewOrdersVO.setCancelledOrders(cancelledOrders);

        //已完成訂單
        Integer completedOrders = orderMapper.countByStatusWithDate(Orders.COMPLETED, begin, end);
        overviewOrdersVO.setCompletedOrders(completedOrders);

        //待派送訂單
        Integer deliveredOrders = orderMapper.countByStatusWithDate(Orders.CONFIRMED, begin, end);
        overviewOrdersVO.setDeliveredOrders(deliveredOrders);

        //待接單
        Integer waitingOrders = orderMapper.countByStatusWithDate(Orders.TO_BE_CONFIRMED, begin, end);
        overviewOrdersVO.setWaitingOrders(waitingOrders);

        return overviewOrdersVO;
    }

    @Override
    public OverviewDishesVO getOverviewDishes() {
        OverviewDishesVO overviewDishesVO = new OverviewDishesVO();
        Integer discontinued = dishMapper.countDish(StatusConstant.DISABLE);
        overviewDishesVO.setDiscontinued(discontinued);

        Integer sole = dishMapper.countDish(StatusConstant.ENABLE);
        overviewDishesVO.setSold(sole);
        return overviewDishesVO;
    }

    @Override
    public OverviewDishesVO getOverviewSetmeals() {
        OverviewDishesVO overviewDishesVO = new OverviewDishesVO();
        Integer discontinued = setmealMapper.countSetmeal(StatusConstant.DISABLE);
        discontinued = discontinued == null ? 0 : discontinued;
        overviewDishesVO.setDiscontinued(discontinued);

        Integer sole = setmealMapper.countSetmeal(StatusConstant.ENABLE);
        overviewDishesVO.setSold(sole);
        return overviewDishesVO;
    }
}
